﻿using Abp.Collections.Extensions;
using Abp.Dependency;
using Abp.Domain.Repositories;
using Abp.Domain.Services;
using Castle.Core.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DevM.Development.DataMigration
{
    public class DataMigrationManager: ITransientDependency, IDomainService
    {
        private readonly IRepository<Config, long> configRepository;
        private readonly IRepository<Data, long> dataRepository;
        private readonly IRepository<Database, long> databaseRepository;
        private readonly ILogger logger;

        public DataMigrationManager(IRepository<Config, long> configRepository, IRepository<Data, long> dataRepository, IRepository<Database, long> databaseRepository, ILogger logger)
        {
            this.configRepository = configRepository;
            this.dataRepository = dataRepository;
            this.databaseRepository = databaseRepository;
            this.logger = logger;
        }


        /// <summary>
        /// 配置列表
        /// </summary>
        public IQueryable<Config> Configs { get => configRepository.GetAll(); }

        /// <summary>
        /// 添加配置
        /// </summary>
        /// <param name="config"></param>
        /// <returns></returns>
        public async Task<Config> AddConfig(Config config)
        {
            config.CreationTime = DateTime.Now;
            return await configRepository.GetAsync(await configRepository.InsertAndGetIdAsync(config));
        }
        /// <summary>
        /// 更新配置信息
        /// </summary>
        /// <param name="config"></param>
        /// <returns></returns>
        public async Task<Config> UpdateConfig(Config config)
        {
            var _config = await configRepository.GetAsync(config.Id);
            _config.LastModificationTime = DateTime.Now;
            _config.FileName = config.FileName;
            _config.Description = config.Description;
            _config.ActionDetail = config.ActionDetail;
            _config.ActionType = config.ActionType;
            _config.Memo = config.Memo;
            return await configRepository.UpdateAsync(_config);
        }
        /// <summary>
        /// 删除配置
        /// </summary>
        /// <param name="deleterUserId"></param>
        /// <param name="configId"></param>
        /// <returns></returns>
        public async Task DeleteConfig(params long[] configId)
        {
            foreach (var id in configId)
            {
                await configRepository.DeleteAsync(id);
            }
        }

        /// <summary>
        /// 数据列表
        /// </summary>
        public IQueryable<Data> Datas { get => dataRepository.GetAll(); }

        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="config"></param>
        /// <returns></returns>
        public async Task<Data> AddData(Data data)
        {
            data.CreationTime = DateTime.Now;
            return await dataRepository.GetAsync(await dataRepository.InsertAndGetIdAsync(data));
        }
        /// <summary>
        /// 更新数据信息
        /// </summary>
        /// <param name="config"></param>
        /// <returns></returns>
        public async Task<Data> UpdateData(Data data)
        {
            var _data =await dataRepository.GetAsync(data.Id);
            _data.LastModificationTime = DateTime.Now;
            _data.ActionDetail = data.ActionDetail;
            _data.ActionType = data.ActionType;
            _data.Description = data.Description;
            _data.Memo = data.Memo;
            return await dataRepository.UpdateAsync(_data);
        }
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="deleterUserId"></param>
        /// <param name="dynamicInfomationId"></param>
        /// <returns></returns>
        public async Task DeleteData(params long[] dataId)
        {
            foreach (var id in dataId)
            {
                await dataRepository.DeleteAsync(id);
            }
        }

        /// <summary>
        /// 数据列表
        /// </summary>
        public IQueryable<Database> Databases { get => databaseRepository.GetAll(); }

        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="config"></param>
        /// <returns></returns>
        public async Task<Database> AddDatabase(Database database)
        {
            database.CreationTime = DateTime.Now;
            return await databaseRepository.GetAsync(await databaseRepository.InsertAndGetIdAsync(database));
        }
        /// <summary>
        /// 更新数据信息
        /// </summary>
        /// <param name="config"></param>
        /// <returns></returns>
        public async Task<Database> UpdateDatabase(Database database)
        {
            var _database =await databaseRepository.GetAsync(database.Id);
            _database.LastModificationTime = DateTime.Now;
            _database.Description = database.Description;
            _database.Memo = database.Memo;
            _database.Sql = database.Sql;
            return await databaseRepository.UpdateAsync(_database);
        }
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="databaseId"></param>
        /// <returns></returns>
        public async Task DeleteDatabase(params long[] databaseId)
        {
            foreach (var id in databaseId)
            {
                await databaseRepository.DeleteAsync(id);
            }
        }

        public async Task<int> GetConfigsCount(long releaseRecordId)
        {
            return configRepository.GetAll().WhereIf(releaseRecordId > 0, c => c.ReleaseRecordId == releaseRecordId).Count();
        }
        public async Task<int> GetDatasCount(long releaseRecordId)
        {
            return dataRepository.GetAll().WhereIf(releaseRecordId > 0, c => c.ReleaseRecordId == releaseRecordId).Count();
        }
        public async Task<int> GetDatabasesCount(long releaseRecordId)
        {
            return databaseRepository.GetAll().WhereIf(releaseRecordId > 0, c => c.ReleaseRecordId == releaseRecordId).Count();
        }
    }
}
